# online_answer_records/schemas.py
from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime


class OnlineAnswerRecordBase(BaseModel):
    user_id: int = Field(..., description="用户ID")
    question_id: int = Field(..., description="题目ID")
    content: str = Field(..., description="题目内容")
    answer: str = Field(..., description="题目答案")
    user_answer: str = Field(..., description="用户回答的答案")
    is_correct: bool = Field(..., description="是否答对")
    time_taken: int = Field(..., description="答题用时（单位：秒）")
    session_id: Optional[str] = Field(None, max_length=100, description="答题会话ID")


class OnlineAnswerRecordCreate(OnlineAnswerRecordBase):
    pass


class OnlineAnswerRecordUpdate(OnlineAnswerRecordBase):
    # 更新时可以选择不更新所有字段
    user_id: Optional[int] = None
    question_id: Optional[int] = None
    content: Optional[str] = None
    answer: Optional[str] = None
    user_answer: Optional[str] = None
    is_correct: Optional[bool] = None
    time_taken: Optional[int] = None


class OnlineAnswerRecord(OnlineAnswerRecordBase):
    id: int
    answered_at: datetime
    
    class Config:
        from_attributes = True